package com.xu.common.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 徐老板
 * @date 2020/4/1   17:30
 */
public class HttpUtil {
    //获取第三方接口服务 感觉feign就行 不过这个不是自己的了路径
    //所以要用这种原生的

    /**
     * get方式获取json
     */
    public static String getJson(String url){
        //说明请求方式
        HttpGet get =new HttpGet(url);
        //创建客户端对象
        HttpClient client = HttpClientBuilder.create().build();

        try {
            //获取请求结果
            HttpResponse response = client.execute(get);
            //校验http状态码
            if (response.getStatusLine().getStatusCode()==200){
                //获取响应的内容对象
                HttpEntity entity = response.getEntity();
                //转换结果为字符串  这个工具类倒是第一次见
                return EntityUtils.toString(entity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * post方式获取json  这个都是请求第三方接口用的
     */
    public static String postJson(String url,String paramjson){
        HttpPost post = new HttpPost(url);
        HttpClient client = HttpClientBuilder.create().build();
        //设置post请求的请求参数
        //像这些都可以直接拿过来用
        //这里要设置编码格式为json 不然传递到第三方接口显示为乱码
        //其实与get相比 就多了设置参数这一步
        StringEntity stringEntity = new StringEntity(paramjson, ContentType.APPLICATION_JSON);
        post.setEntity(stringEntity);

        try {
            HttpResponse response = client.execute(post);
            if (response.getStatusLine().getStatusCode()==200){
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * post请求方式传递键值对 获取第三方接口信息
     */
    public static String postJson(String url, Map<String,String> map){
        HttpPost post = new HttpPost(url);
        HttpClient client = HttpClientBuilder.create().build();
        List<NameValuePair> pairList = new ArrayList<>();
        for (String key:map.keySet()){
            pairList.add(new BasicNameValuePair(key, map.get(key)));
        }
        try {
            HttpEntity httpEntity = new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8"));
            post.setEntity(httpEntity);
            HttpResponse response = client.execute(post);
            if (response.getStatusLine().getStatusCode()==200){
                HttpEntity entity = response.getEntity();
                return EntityUtils.toString(entity);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 这里直接测试一下
     */
//    public static void main(String[] args) {
////        System.out.println(getJson("https://way.jd.com/jisuapi/weather?city=北京&appkey=9878b9b510123e52a951fe2074d19be1"));
//        Map<String,String> map =new HashMap<>();
//        map.put("city", "北京");
//        map.put("appkey", "9878b9b510123e52a951fe2074d19be1");
//        System.out.println(postJson("https://way.jd.com/jisuapi/weather",map));
//    }

    //写到这里 common部分就暂时写完了

}
